home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / ax25hdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  2.4 KB  |  107 lines

  1. #include "global.h"
  2. #include "mbuf.h"
  3. #include "ax25.h"
  4.  
  5. /* Convert a host-format AX.25 header into a mbuf ready for transmission */
  6. struct mbuf *
  7. htonax25(hdr,data)
  8. register struct ax25 *hdr;
  9. struct mbuf *data;
  10. {
  11.     struct mbuf *bp;
  12.     register char *cp;
  13.     register int16 i;
  14.  
  15.     if(hdr == (struct ax25 *)NULL || hdr->ndigis > MAXDIGIS)
  16.         return NULLBUF;
  17.  
  18.     /* Allocate space for return buffer */
  19.     i = AXALEN * (2 + hdr->ndigis);
  20.     if((bp = pushdown(data,i)) == NULLBUF)
  21.         return NULLBUF;
  22.  
  23.     /* Now convert */
  24.     cp = bp->data;        /* cp -> dest field */
  25.  
  26.     /* Generate destination field */
  27.     memcpy(cp,hdr->dest,AXALEN);
  28.     if(hdr->cmdrsp == LAPB_COMMAND)
  29.         cp[ALEN] |= C;    /* Command frame sets C bit in dest */
  30.     else
  31.         cp[ALEN] &= ~C;
  32.     cp[ALEN] &= ~E;    /* Dest E-bit is always off */
  33.  
  34.     cp += AXALEN;        /* cp -> source field */
  35.  
  36.     /* Generate source field */
  37.     memcpy(cp,hdr->source,AXALEN);
  38.     if(hdr->cmdrsp == LAPB_RESPONSE)
  39.         cp[ALEN] |= C;
  40.     else
  41.         cp[ALEN] &= ~C;
  42.     /* Set E bit on source address if no digis */
  43.     if(hdr->ndigis == 0){
  44.         cp[ALEN] |= E;
  45.         return bp;
  46.     }
  47.  
  48.     cp += AXALEN;        /* cp -> first digi field */
  49.  
  50.     /* All but last digi get copied with E bit off */
  51.     for(i=0; i < hdr->ndigis; i++){
  52.         memcpy(cp,hdr->digis[i],AXALEN);
  53.         if(i < hdr->ndigis - 1)
  54.             cp[ALEN] &= ~E;
  55.         else
  56.             cp[ALEN] |= E;    /* Last digipeater has E bit set */
  57.         if(i < hdr->nextdigi)
  58.             cp[ALEN] |= REPEATED;
  59.         else
  60.             cp[ALEN] &= ~REPEATED;
  61.         cp += AXALEN;        /* cp -> next digi field */
  62.     }
  63.     return bp;
  64. }
  65. /* Convert a network-format AX.25 header into a host format structure
  66.  * Return -1 if error, number of addresses if OK
  67.  */
  68. int
  69. ntohax25(hdr,bpp)
  70. register struct ax25 *hdr;    /* Output structure */
  71. struct mbuf **bpp;
  72. {
  73.     register char *axp;
  74.  
  75.     if(pullup(bpp,hdr->dest,AXALEN) < AXALEN)
  76.         return -1;
  77.  
  78.     if(pullup(bpp,hdr->source,AXALEN) < AXALEN)
  79.         return -1;
  80.  
  81.     /* Process C bits to get command/response indication */
  82.     if((hdr->source[ALEN] & C) == (hdr->dest[ALEN] & C))
  83.         hdr->cmdrsp = LAPB_UNKNOWN;
  84.     else if(hdr->source[ALEN] & C)
  85.         hdr->cmdrsp = LAPB_RESPONSE;
  86.     else
  87.         hdr->cmdrsp = LAPB_COMMAND;
  88.  
  89.     hdr->ndigis = 0;
  90.     hdr->nextdigi = 0;
  91.     if(hdr->source[ALEN] & E)
  92.         return 2;    /* No digis */
  93.  
  94.     /* Count and process the digipeaters */
  95.     axp = hdr->digis[0];
  96.     while(hdr->ndigis < MAXDIGIS && pullup(bpp,axp,AXALEN) == AXALEN){
  97.         hdr->ndigis++;
  98.         if(axp[ALEN] & REPEATED)
  99.             hdr->nextdigi++;
  100.         if(axp[ALEN] & E)    /* Last one */
  101.             return hdr->ndigis + 2;            
  102.         axp += AXALEN;
  103.     }
  104.     return -1;    /* Too many digis */
  105. }
  106.  
  107.